//
//  MusicDataBase.m
//  哎游
//
//  Created by 康守帅 on 15-1-13.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "MusicDataBase.h"
#import "DB.h"

@implementation MusicDataBase

+ (void)insertIntoMusic:(MusicModel *)music user:(NSString *)user
{
    sqlite3 *dbPoint = [DB openDB];
    NSString *sqlStr = [NSString stringWithFormat:@"insert into userMusic(music_id, music_title, music_author, music_url, music_imageUrl, user) values ('%@', '%@', '%@', '%@', '%@', '%@')", [music.trackId stringValue], music.title, music.nickname, music.playUrl64, music.imageUrl, user];
    
    int result = sqlite3_exec(dbPoint, [sqlStr UTF8String], nil, nil, nil);
    if (result == SQLITE_OK) {
        NSLog(@"插入成功");
    }else {
        NSLog(@"插入失败");
    }
}

+ (void)deleteFromUserMusic:(NSString *)musicId user:(NSString *)user
{
    sqlite3 *dbPoint = [DB openDB];
    NSString *sqlStr = [NSString stringWithFormat:@"delete from userMusic where music_id = '%@' and user = '%@'", musicId, user];
    int result = sqlite3_exec(dbPoint, [sqlStr UTF8String], nil, nil, nil);
    if (result == SQLITE_OK) {
        NSLog(@"删除成功");
    }else {
        NSLog(@"删除失败");
    }
}

+ (NSArray *)selectWithUserMusic:(NSString *)user
{
    sqlite3 *dbPoint = [DB openDB];
    sqlite3_stmt *stmt = nil;
    NSString *sqlStr = [NSString stringWithFormat:@"select music_id, music_title, music_author, music_url, music_imageUrl from userMusic where user = '%@'", user];
    int result = sqlite3_prepare_v2(dbPoint, [sqlStr UTF8String], -1, &stmt, nil);
    NSMutableArray *array = [NSMutableArray array];
    if (result == SQLITE_OK) {
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            const unsigned char *music_id = sqlite3_column_text(stmt, 0);
            const unsigned char *music_title = sqlite3_column_text(stmt, 1);
            const unsigned char *music_author = sqlite3_column_text(stmt, 2);
            const unsigned char *music_url = sqlite3_column_text(stmt, 3);
            const unsigned char *music_imageUrl = sqlite3_column_text(stmt, 4);
            
            NSString *musicId = [NSString stringWithUTF8String:(const char *)music_id];
            NSString *title = [NSString stringWithUTF8String:(const char *)music_title];
            NSString *author = [NSString stringWithUTF8String:(const char *)music_author];
            NSString *urlStr = [NSString stringWithUTF8String:(const char *)music_url];
            NSString *imageUrl = [NSString stringWithUTF8String:(const char *)music_imageUrl];
            
            MusicModel *music = [MusicModel musciWithMusicID:[NSNumber numberWithInteger:[musicId integerValue]] title:title author:author urlStr:urlStr imageUrl:imageUrl];
            [array addObject:music];
        }
    }
    
    sqlite3_finalize(stmt);
    
    return array;
}

+ (BOOL)selectForMusicId:(NSString *)musicId user:(NSString *)user
{
    BOOL exist = NO;
    sqlite3 *dbPoint = [DB openDB];
    sqlite3_stmt *stmt = nil;
    NSString *sqlStr = [NSString stringWithFormat:@"select music_id, music_title, music_author, music_url from userMusic where music_id = '%@' and user = '%@'", musicId, user];
    NSLog(@"%@", sqlStr);
    int result = sqlite3_prepare_v2(dbPoint, [sqlStr UTF8String], -1, &stmt, nil);
    if (result == SQLITE_OK) {
        if (sqlite3_step(stmt) == SQLITE_ROW) {
            exist = YES;
        }
    }
    sqlite3_finalize(stmt);
    return exist;
}







@end
